package org.util;


import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author aqiang9  2020-11-08
 */
public class Util
{
	public static void print(byte[] bytes)
	{
		StringBuilder sb = new StringBuilder(bytes.length * 2);
		for (byte b : bytes)
		{
			sb.append(b).append(" ");
		}
		System.out.println(sb.toString());
	}

	public static String file2String(final String path)
	{
		StringBuilder sb = new StringBuilder();
		//Files.readAllBytes(Paths.get(path) )
		FileReader reader = null;
		try
		{
			File file = new File(path);
			reader = new FileReader(file);
			char[] buffer = new char[1024];
			System.out.println(file.length());
			int len = -1;
			while ((len = reader.read(buffer)) != -1)
			{
				sb.append(buffer, 0, len);
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			if (reader != null)
			{
				try
				{
					reader.close();
				} catch (IOException ignored) {}
			}
		}
		return sb.toString();
	}

	public static void string2File(String data, String path)
	{
		FileWriter writer = null;
		try
		{
			writer = new FileWriter(new File(path));
			writer.write(data);
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			if (writer != null)
			{
				try
				{
					writer.close();
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}

	}

	public static String inputStream2String(InputStream in) throws IOException
	{
		int len = -1;
		byte[] buffer = new byte[1024];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		while ((len = in.read(buffer)) != -1)
		{
			baos.write(buffer, 0, len);
		}
		baos.close();

		return baos.toString(StandardCharsets.UTF_8.name());
	}
}
